{% extends "base.html" %}

{% block content %}
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>表单列表</h2>
        {% if current_user.is_admin %}
        <a href="{{ url_for('create_form') }}" class="btn btn-primary">创建表单</a>
        {% endif %}
    </div>

    <div class="row">
        {% for form in forms %}
        <div class="col-md-4 mb-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">{{ form.title }}</h5>
                    <p class="card-text">{{ form.description }}</p>
                    <div class="d-flex justify-content-between align-items-center">
                        {% set responses = current_user.form_responses|selectattr('form_id', 'eq', form.id)|list %}
                        {% if responses %}
                            <div>
                                <span class="text-success">已填写 {{ responses|length }} 次</span>
                                <a href="{{ url_for('fill_form', form_id=form.id) }}" class="btn btn-sm btn-outline-primary ms-2">继续填写</a>
                            </div>
                        {% else %}
                            <a href="{{ url_for('fill_form', form_id=form.id) }}" class="btn btn-primary">填写表单</a>
                        {% endif %}
                        {% if current_user.is_admin %}
                        <div class="btn-group">
                            <a href="{{ url_for('export_form_results', form_id=form.id) }}" class="btn btn-success">导出结果</a>
                            <button type="button" class="btn btn-danger" 
                                    onclick="deleteForm({{ form.id }}, '{{ form.title }}')">删除表单</button>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>

<script>
function deleteForm(formId, formTitle) {
    if (confirm(`确定要删除表单"${formTitle}"吗？\n此操作将同时删除所有填写记录且不可恢复！`)) {
        fetch(`/forms/${formId}/delete`, {
            method: 'POST',
        }).then(response => {
            if (response.ok) {
                window.location.reload();
            } else {
                alert('删除失败，请重试');
            }
        }).catch(error => {
            console.error('Error:', error);
            alert('删除失败，请重试');
        });
    }
}
</script>
{% endblock %} 